package com.shushan.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.shushan.base.BaseFragment;
import com.shushan.home.adapter.SubjectRecyclerViewAdapter;
import com.shushan.home.bean.LearnByDoBean;
import com.shushan.pages.NewStudyWithExerciseActivity;
import com.shushan.pages.StudyWithExerciseActivity;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.R;
import com.shushan.ui.SubjectRecyclerViewDivider;
import com.shushan.util.Session;
import com.shushan.util.ToolUtils;

import java.util.ArrayList;


/**
 * @ClassName: SubjectFragment
 * @Desciption: //边学边练的fragment
 * @author: zhangshihao
 * @date: 2018-04-08
 */
public class SubjectFragment extends BaseFragment implements SubjectRecyclerViewAdapter.OnItemClickListener{

    private static final String TAG = SubjectFragment.class.getSimpleName();

    private View mContentView;
    private RecyclerView mRecyclerView;
    private LinearLayout llEmpty;

    private ArrayList<LearnByDoBean> learnByDoBeanList;
    private SubjectRecyclerViewAdapter mSRVAdapter;
    private BabyApplication shuShanApplication = BabyApplication.getInstance();

    private OnFragmentItemClickListener onFragmentItemClickListener;

    private int subjectId ;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        subjectId = getArguments().getInt("subjectId");
        Log.i(TAG,"subjectId=" + subjectId);

        learnByDoBeanList = getArguments().getParcelableArrayList("learnByDoBeanList");

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mContentView = inflater.inflate(R.layout.fragment_subject,null,false);
        initView();
        return mContentView;
    }

    private void initView(){
        mRecyclerView = mContentView.findViewById(R.id.rv_fragment_subject);
        llEmpty = mContentView.findViewById(R.id.ll_empty_state_subject);

        updateUIbyData(learnByDoBeanList);
    }

    private void updateUIbyData(ArrayList<LearnByDoBean> subjectByDoList){
        if(!ToolUtils.isListEmpty(subjectByDoList)){
            llEmpty.setVisibility(View.GONE);
            mRecyclerView.setVisibility(View.VISIBLE);
        }else{
            llEmpty.setVisibility(View.VISIBLE);
            mRecyclerView.setVisibility(View.GONE);
        }
        mSRVAdapter = new SubjectRecyclerViewAdapter(getContext(),subjectByDoList);
        mSRVAdapter.setOnItemClickListener(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.addItemDecoration(new SubjectRecyclerViewDivider(LinearLayoutManager.VERTICAL,32));
        mRecyclerView.setAdapter(mSRVAdapter);
    }

    @Override
    public void onItemClick(View itemView, int position) {
        int childId = shuShanApplication.getCurrentChildId();
        if(!shuShanApplication.isVip() || (childId <= 0)) {
            if(onFragmentItemClickListener != null){
                onFragmentItemClickListener.onFragmentItemClick(itemView,position);
            }
            return;
        }

        Session.getSession().put("learnByDoBeanList",learnByDoBeanList);

        super.setAllowBack(true);

        Intent intent = new Intent(getContext(),NewStudyWithExerciseActivity.class);
        intent.putExtra("position",position);
        intent.putExtra("subjectId",subjectId);
        startActivity(intent);
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    public int getSubjectId() {
        return subjectId;
    }


    public SubjectRecyclerViewAdapter getmSRVAdapter() {
        return mSRVAdapter;
    }

    public void setmSRVAdapter(SubjectRecyclerViewAdapter mSRVAdapter) {
        this.mSRVAdapter = mSRVAdapter;
    }

    public void clearUI(){
        llEmpty.setVisibility(View.VISIBLE);
        mRecyclerView.setVisibility(View.GONE);
    }

    public OnFragmentItemClickListener getOnFragmentItemClickListener() {
        return onFragmentItemClickListener;
    }

    public void setOnFragmentItemClickListener(OnFragmentItemClickListener onFragmentItemClickListener) {
        this.onFragmentItemClickListener = onFragmentItemClickListener;
    }
}
